package com.sctu.EMSApp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.bean.User;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;
import com.sctu.util.SharePreferenceUtil;

import java.util.HashMap;

/**
 * Created by zhu on 2016/1/5.
 */
public class EditActivity  extends FragmentActivity implements View.OnClickListener{

    private ImageView returnButton,editMsg,editPassword;
    private TextView nameText, usernameText;
    private EditText nickname,sex,age,birthday,work,city,email;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_edit);
        setView();
        setEven();
        setData();
    }

    private void setData() {
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_USER_SHOW+ SharePreferenceUtil.getInstance(EditActivity.this).getKeyData("userId"),showListener,showerrorListener);
    }

    private void setEven() {
        returnButton.setOnClickListener(this);
        editMsg.setOnClickListener(this);
        editPassword.setOnClickListener(this);
    }

    private void setView() {
        returnButton = (ImageView) findViewById(R.id.my_edit_return_button);
        editMsg = (ImageView) findViewById(R.id.my_save_msg_image);
        editPassword = (ImageView) findViewById(R.id.my_edit_password_image);

        nickname = (EditText) findViewById(R.id.my_edit_nickname);
        sex = (EditText) findViewById(R.id.my_edit_sex);
        age = (EditText) findViewById(R.id.my_edit_age);
        birthday = (EditText) findViewById(R.id.my_edit_year);
        work = (EditText) findViewById(R.id.my_edit_work);
        city = (EditText) findViewById(R.id.my_edit_city);
        email = (EditText) findViewById(R.id.my_edit_email);

        nameText = (TextView) findViewById(R.id.my_edit_name);
        usernameText = (TextView) findViewById(R.id.my_edit_username);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.my_edit_return_button:
                finish();
                break;
            
            case R.id.my_save_msg_image:
                HashMap<String,String> msgMap = new HashMap<String, String>();
                msgMap.put("username",usernameText.getText().toString());
                msgMap.put("age",age.getText().toString());
                msgMap.put("sex",sex.getText().toString());
                msgMap.put("nickname",nickname.getText().toString());
                msgMap.put("birthday",birthday.getText().toString());
                msgMap.put("city",city.getText().toString());
                msgMap.put("email",email.getText().toString());
                msgMap.put("profession",work.getText().toString());
                GetVolleyHttp.getInstance(this).
                        getStringPOSTJsonData(UrlConfig.URL_USER_UPDATE + SharePreferenceUtil.getInstance(EditActivity.this).getKeyData("userId"),msgMap,updateListener,updateerrorListener);
                break;
            
            case R.id.my_edit_password_image:
                
                break;
            
        }
    }

    Response.Listener<String> showListener = new Response.Listener<String>() {

        @Override
        public void onResponse(String s) {
            Log.e("heheda", s);

            User userJsonListBean= JSON.parseObject(s, User.class);
            if (userJsonListBean.getCode() == 0){
                User.Result result = userJsonListBean.getResult().get(0);
                nickname.setText(result.getNickname());
                sex.setText(result.getSex());
                age.setText(String.valueOf(result.getAge()));
                birthday.setText(result.getBirthday());
                work.setText(result.getProfession());
                city.setText(result.getCity());
                email.setText(result.getEmail());
                nameText.setText(result.getNickname());
                usernameText.setText(result.getUsername());
            }else {
                Toast.makeText(EditActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
            }


        }
    };

    Response.ErrorListener showerrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            if (SharePreferenceUtil.getInstance(EditActivity.this).getKeyData("userId").equals("")){
                Toast.makeText(EditActivity.this, "登陆后才可访问", Toast.LENGTH_LONG).show();
                finish();
            }else {
                Toast.makeText(EditActivity.this, "网络出错", Toast.LENGTH_LONG).show();
            }

        }
    };


    Response.Listener<String> updateListener = new Response.Listener<String>() {

        @Override
        public void onResponse(String s) {
            Log.e("heheda", s);

            User userJsonListBean= JSON.parseObject(s, User.class);
            if (userJsonListBean.getCode() == 0){
                Toast.makeText(EditActivity.this, "更新成功", Toast.LENGTH_LONG).show();
                finish();
            }else {
                Toast.makeText(EditActivity.this, "更新失败", Toast.LENGTH_LONG).show();
            }


        }
    };

    Response.ErrorListener updateerrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            if (SharePreferenceUtil.getInstance(EditActivity.this).getKeyData("userId").equals("")){
                Toast.makeText(EditActivity.this, "登陆后才可访问", Toast.LENGTH_LONG).show();
                finish();
            }else {
                Toast.makeText(EditActivity.this, "网络出错", Toast.LENGTH_LONG).show();
            }

        }
    };
    
}
